import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit';

// 仪表盘数据接口
export interface DashboardData {
  totalUsers: number;
  activeUsers: number;
  totalOrders: number;
  totalRevenue: number;
  recentActivities: Activity[];
  chartData: ChartData[];
}

// 活动接口
interface Activity {
  id: number;
  type: string;
  description: string;
  timestamp: string;
  user: string;
}

// 图表数据接口
interface ChartData {
  date: string;
  value: number;
  category: string;
}

// 仪表盘状态接口
interface DashboardState {
  data: DashboardData | null;
  loading: boolean;
  error: string | null;
}

// 初始状态
const initialState: DashboardState = {
  data: null,
  loading: false,
  error: null,
};

// 异步 action：获取仪表盘数据
export const fetchDashboardData = createAsyncThunk<
  DashboardData,
  void,
  { rejectValue: string }
>('dashboard/fetchData', async (_, { rejectWithValue }) => {
  try {
    // 模拟 API 调用
    const response = await new Promise<DashboardData>((resolve) => {
      setTimeout(() => {
        resolve({
          totalUsers: 1250,
          activeUsers: 890,
          totalOrders: 3456,
          totalRevenue: 125000,
          recentActivities: [
            {
              id: 1,
              type: 'user_registration',
              description: '新用户注册',
              timestamp: '2023-12-01 10:30:00',
              user: '张三',
            },
            {
              id: 2,
              type: 'order_created',
              description: '新订单创建',
              timestamp: '2023-12-01 09:15:00',
              user: '李四',
            },
            {
              id: 3,
              type: 'payment_received',
              description: '收到付款',
              timestamp: '2023-12-01 08:45:00',
              user: '王五',
            },
          ],
          chartData: [
            { date: '2023-11-25', value: 120, category: '用户' },
            { date: '2023-11-26', value: 135, category: '用户' },
            { date: '2023-11-27', value: 150, category: '用户' },
            { date: '2023-11-28', value: 140, category: '用户' },
            { date: '2023-11-29', value: 160, category: '用户' },
            { date: '2023-11-30', value: 175, category: '用户' },
            { date: '2023-12-01', value: 180, category: '用户' },
          ],
        });
      }, 1000);
    });
    return response;
  } catch (error) {
    return rejectWithValue('获取仪表盘数据失败');
  }
});

// 创建仪表盘 slice
const dashboardSlice = createSlice({
  name: 'dashboard',
  initialState,
  reducers: {
    // 清除错误
    clearError: (state) => {
      state.error = null;
    },
  },
  extraReducers: (builder) => {
    builder
      .addCase(fetchDashboardData.pending, (state) => {
        state.loading = true;
        state.error = null;
      })
      .addCase(fetchDashboardData.fulfilled, (state, action) => {
        state.loading = false;
        state.data = action.payload;
      })
      .addCase(fetchDashboardData.rejected, (state, action) => {
        state.loading = false;
        state.error = action.payload as string;
      });
  },
});

// 导出 actions
export const { clearError } = dashboardSlice.actions;

// 导出 reducer
export default dashboardSlice.reducer; 